package crp.services.api.graphql.models;

import crp.core.security.domain.models.AccessToken;
import crp.core.security.domain.models.CallerPrincipal;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import org.eclipse.microprofile.graphql.Ignore;
import org.eclipse.microprofile.graphql.Type;

import javax.validation.constraints.NotNull;
import java.util.Objects;

@Type("AccessToken")
@Builder
@Getter
@AllArgsConstructor
public class AccessTokenPayload {
    @Ignore
    @NotNull
    private final String id;
    @Ignore
    @NotNull
    private final String type;
    @NotNull
    private final String accessToken;
    @NotNull
    private final long expiresIn;

    public static AccessTokenPayload of(AccessToken token) {
        return AccessTokenPayload.builder()
                .id(token.getId())
                .type(token.getType())
                .accessToken(token.getAccessToken())
                .expiresIn(token.getExpiresIn())
                .build();
    }

    public boolean isUser() {
        return Objects.equals(type, CallerPrincipal.TYPE_USER);
    }
}
